package com.oceansoft.mobile.biz.front.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.oceansoft.mobile.biz.api.entity.Member;
import com.oceansoft.mobile.common.Constant;
import com.oceansoft.mobile.common.util.HttpClientUtil;
import com.oceansoft.mobile.common.util.Result;
import com.oceansoft.mobile.common.util.StringUtils;
import com.oceansoft.mobile.weixin.util.DesUtil;
import com.oceansoft.mobile.weixin.util.WeixinUtil;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import static org.springframework.web.bind.annotation.RequestMethod.POST;

/**
 * Created by Administrator on 2016/1/20.
 */
@Controller
@RequestMapping("jguser")
public class JgUserController {
    public static String JGUserLogin = Constant.host5 + "api/users/JGUserLogin";
    public static String JGOpenID = Constant.host5 + "api/users/JGOpenID";
    /**
     * 登录认证
     *
     * @param user 登录用户
     * @return Result
     */
    @ResponseBody
    @RequestMapping(value = "login", method = POST)
    public Result loginAuth(@RequestParam("USERNAME") final String USERNAME,
                            @RequestParam("PASSWORD") final String PASSWORD) throws IOException {
        try {
            DesUtil tools = new DesUtil();
            Map<String, String> param = new HashMap<String, String>();
            param.put("USERNAME", USERNAME);
            param.put("PASSWORD", tools.encode(PASSWORD));
            Result re = JSONObject.parseObject(HttpClientUtil.doPost(JGUserLogin, param, "utf-8"), Result.class);
            return re;
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(Result.FAILURE, "用户登录失败");
        }
    }
    /**
     * 登录认证
     *
     * @param user 登录用户
     * @return Result
     */
    @ResponseBody
    @RequestMapping(value = "auth", method = POST)
    public Result openIdlogin(@RequestParam("OpenId") final String OpenId) throws IOException {
        try {
            Result re = JSONObject.parseObject(HttpClientUtil.doGet(JGOpenID, "OpenId",OpenId), Result.class);
            return re;
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(Result.FAILURE, "查询失败");
        }
    }

}
